//
//  JYZHttpHeader.m
//  NetWorking
//
//  Created by dbjyz on 16/3/29.
//  Copyright © 2016年 dbjyz. All rights reserved.
//

#import "JYZHttpHeader.h"

@implementation JYZHttpHeader

static JYZHttpHeader *instance = nil;

+ (JYZHttpHeader *)shareInstance{
    if (instance == nil) {
        instance = [[super alloc]init];
    }
    return instance;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.version         = @"1.2"; //接口版本
        self.phoneversion    = @"";//手机型号
        self.mobiletype      = @"0";//0.iOS 1.安卓,2,其他
        self.systemversion   = @"";//系统型号
        long timestamp       = (long)[[NSDate date] timeIntervalSince1970];
        self.appdate         = [NSString stringWithFormat:@"%ld",timestamp];//时间戳
        self.softversion     = @"";//App的软件版本
        self.currentlocation = @"";
        self.deviceid        = @"";
        self.getuiid         = @"";
        self.userid          = @"";
        self.city            = @"";
        self.cityid          = @"";
        self.longitude       = @"";
        self.latitude        = @"";
        
    }
    return self;
}


-(NSMutableDictionary *)httpHeaderDic {
    
    //获取cookie
    NSString * state = [[NSUserDefaults standardUserDefaults] objectForKey:@"cookie"];
    if (state == NULL) {
        //获取cookie
        NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        if ([cookieJar cookies].count > 0) {
            NSHTTPCookie *cookie = [[cookieJar cookies] objectAtIndex:0];
            [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@=%@",cookie.name,cookie.value] forKey:@"cookie"];
            self.CookieOnce = [NSString stringWithFormat:@"%@=%@",cookie.name,cookie.value];
        }
        
    }else{
        self.CookieOnce = state;
    }
    
    if (_httpHeaderDic == nil) {
        
        @try{
            NSDictionary *baseDic =@{
                                     @"version"         : self.version ,
                                     @"phoneversion"    : self.phoneversion ,
                                     @"mobiletype"      : self.mobiletype ,
                                     @"systemversion"   : self.systemversion ,
                                     @"currentlocation" : self.currentlocation ,
                                     @"deviceid"        : self.deviceid ,
                                     @"getuild"         : self.getuiid ,
                                     @"appdate"         : self.appdate ,
                                     @"userid"          : self.userid ,
                                     @"city"            : self.city ,
                                     @"cityid"          : self.cityid ,
                                     @"longitude"       : self.longitude ,
                                     @"latitude"        : self.latitude ,
                                     @"softversion"     : self.softversion,
                                     };
            _httpHeaderDic = [NSMutableDictionary dictionaryWithDictionary:baseDic];
            
        }
        @catch(NSException *exception) {
            NSLog(@"exception:%@", exception);
        }
        @finally {
            
        }
        
        
        if (self.CookieOnce != nil) {
            [_httpHeaderDic setObject:self.CookieOnce forKey:@"Cookie"];
        }
    }
    
    return _httpHeaderDic;
    
}

@end
